<!-- 
 Copyright 2009 University of  Washington
This code is released under the GPL version 3 - with a copy of the license included
@author Maya Hao Li
 -->

<%
	Page spPage = new Page();
	spPage.addJsLibrary("/javascript/lib/scriptaculous/src/effects.js");
	spPage
			.addJsLibrary("/javascript/lib/scriptaculous/src/scriptaculous.js");
	spPage.addJsLibrary("/javascript/instance.js");
	spPage.addJsLibrary("/javascript/lib/jscommon.js");
	spPage.addJsLibrary("/javascript/calendar.js");
	spPage.addJsLibrary("/javascript/lib/prototype.js");
	spPage.setTitle("Seedpod instance");

	LimsSession spSession = LimsSession.getSession(request);
	SeedpodUser user = spSession.getUser();

	out.println(spPage.getPageOpenHtml());
	out.println(spPage.getBanner(user));

	/* CONTAINER */
	out.println("<div id=\"container\">");

	try {
		InstanceRenderer spInstanceViewer = new InstanceRenderer(request);
		SeedpodDO spObject = spInstanceViewer.getSeedpodDO();
		ClsMap cmap = spObject.getMetaCls();

		if (spObject.getOid() != 0)
			spObject.retrieveById();

		/* OBJECT LABEL, generate buttons for meta information and delete*/
		HtmlLink cmapLink = new HtmlLink("#", cmap.getName());
		cmapLink
				.setJsCommand("onclick=\"Effect.toggle('clsDefinition','appear'); return false;\"");
		Label objTypeLabel = new Label(HtmlUtil.PARAN("Instance Type: "
				+ cmapLink.render()));
		objTypeLabel.setCssClass("clsType");

		/* RIGHT RAIL */
		out.println("<div id=\"rightRail\">");

		// TOOLS 
		out.println("<div id=\"toolBox\" class=\"widgetBox\">");
		out.println("<div class=\"widgetBoxTitle\" >Tools</div>");
		out.println("<div class=\"widgetBoxContent\" id=\"tools\">");
		out.println("<script>loadTools('"
				+ spInstanceViewer.getClsName() + "', "
				+ spInstanceViewer.getOid() + ");</script>");
		out.println("</div>"); // end widgetBoxContent
		out.println("</div>"); // end toolBox div

		// META information ... when was it created?
		out.println("<div class=\"widgetBox\" id=\"metaInfo\">");
		out.println("<div class=\"widgetBoxTitle\">Information</div>");
		out.println("<div class=\"widgetBoxContent\" id=\"meta\">");
		out.println("<script>loadMetaInfo('"
				+ spInstanceViewer.getClsName() + "', "
				+ spInstanceViewer.getOid() + ");</script>");
		out.println("</div>");
		out.println("</div>"); // end widgetBox info

		// Recent instances 
		out.println("<div id=\"recentBox\" class=\"widgetBox\">");
		out
				.println("<div class=\"widgetBoxTitle\">Recent Instances</div>");
		out
				.println("<div class=\"widgetBoxContent\" id=\"recentInstances\">");
		out.println("<li>Add query result of access log</LI>");
		out.println("</div>"); // end recentInstances
		out.println("</div>"); // end recentBox
		
		out.println("<div style=\"height:100%\"></div>");

		out.println("</div>"); // end right rail

		/* CONTENT*/
		out.println("<div id=\"content\">");

		String msg = spInstanceViewer.getPageMsg();
		if (msg != null) {
			out
					.println("<div id=\"pageMsg\" onload=\"Effect.toggle('clsSelectionBox', 'slide'); return false>\">");
			out.println(msg);
			out.println("</div>");
			spInstanceViewer.clearPageMsg();
			spSession.clearAttribute(InstanceRenderer.PAGE_MSG);
		}

		// OBJECT TYPE LABEL
		out.println("<div class=\"instanceLabel\">");
		out.println(spInstanceViewer.getLabel());
		out.println(spInstanceViewer.getType());

		// OBJECT EDIT LINK
		if (user.canEditData() && spInstanceViewer.doView() ) {
			out.println(spInstanceViewer.getEditHtmlLink());
		}

		// DELETE LINK: if object is persistent, print delete link
		if (user.canEditData() && spObject.isPersistent()) {
			out.println(spInstanceViewer.getDeleteHtmlLink());
		} 
		
		// NO ACTION 
		if ( spObject.isDeleted() ) {
			out.println( "<span class=errorBox_small>Data marked for DELETION can no longer be edited.</SPAN>");
		} else if (!spObject.isPersistent()) {
			out.println("[ <span class=errorBox_small>unsaved</span> ]");
		}

		out.println("</div>"); // end instanceLabel div
		out.println(HtmlUtil.NL()); // blank line below label

		// INSTANCE EDIT/DATA TABLE
		out.println("<div id=\"instance\" >");
		if (spInstanceViewer.doEdit()
				|| spInstanceViewer.hasValidationError()) { // EDIT
			if (spInstanceViewer.hasValidationError()) {
				out
						.println(HtmlUtil
								.errorMsg("Please correct the data entry errors before proceeding to save."));
			}
%>
<jsp:include page="formEditInstance.jsp" flush="true"></jsp:include>
<%
	} else if (spInstanceViewer.doCreate()) {
			// CREATE INSTANCE
%>
<jsp:include page="formNewInstance.jsp" flush="true"></jsp:include>
<%
	} else { // defaut action VIEW INSTANCE
			out.println(spInstanceViewer.renderInstance());
		}
		out.println("</div>"); // end of instance
		out.println("</div>"); // end content

	} catch (SeedpodException se) {
		 out.println(se.getMessage());
	}
	out.println("</div>"); // end container

	out.println(spPage.getFooter());
	out.println(spPage.getPageEndHtml());
%>




<%@page import="seedpod.webapp.view.Page"%>
<%@page import="seedpod.webapp.LimsSession"%>
<%@page
	import="seedpod.model.custom.SeedpodUser"%>
<%@page
	import="seedpod.webapp.view.htmlwidget.Label"%>
<%@page import="seedpod.webapp.view.HtmlUtil"%>
<%@page
	import="seedpod.webapp.view.htmlwidget.HtmlLink"%>
<%@page import="seedpod.kb2db.ClsMap"%>
<%@page import="seedpod.model.SeedpodDO"%>
<%@page
	import="seedpod.exceptions.SeedpodException"%>
<%@page
	import="seedpod.webapp.view.InstanceRenderer"%>

